<?php 
	class com_comment extends component
	{
		private $post_id;
		public function get_content()
		{
			$this->count_comments($this->post_id);
			$this->get_comment_form($this->post_id);
		}
		//Set the post id
		public function set_post_id($id)
		{
			$this->post_id = $id;
		}
		//Count the comment
		private function count_comments($param)
		{
			print_r($this->post_id);
			$query = "SELECT c.id FROM comments as c WHERE c.approve = 1 AND c.post_id=".$param;
			$result = mysql_query($query) or die(mysql_error());
			$count = mysql_num_rows($result);
			if ($count == 0)
				echo "<h3 class='com-title'>No comments</h3>";
			else {
				if ($count == 1)
					echo "<h3 class='com-title'>1 comment</h3>";
				else
					echo "<h3 class='com-title'>".$count." comments</h3>";
				//Show the comment
				$this->get_comments($param);
			}
		}
		//Comment form
		private function get_comment_form($param)
		{
			$actionUrl = "index.php?option=com_content&view=article&id=".$param;
?>
			<div id="comment-form">
				<h3 class="com-title">Leave a comment</h3>
				<form method="post" action="<?php echo $actionUrl; ?>" name="comment-form">
					<label for="name">Name:</label>
					<input type="text" name="name" id="name" />
					<label for="email">Email:</label>
					<input type="text" name="email" id="email" />
					<label for="message">Message:</label>
					<textarea name="comment" id="comment"></textarea>
					<input type="submit" id="submit" value="Publish" class="button" />
					<input type="hidden" name="post-id" value="<?php echo $param; ?>" />
				</form>
			</div><!-- End #comment-form-->
<?php
		}
		//Get all comment
		private function get_comments($param)
		{
			$query = "SELECT root.id,root.level,root.email,parent1.created_by as reply_to, root.created_by as author, root.created_on, root.message, root.updated_on AS root_date, parent1.updated_on AS parent1_date, parent2.updated_on AS parent2_date
						FROM comments AS root
						LEFT JOIN comments AS parent1 ON root.parent_id = parent1.id
						LEFT JOIN comments AS parent2 ON parent1.parent_id = parent2.id
						WHERE root.post_id=$param AND root.approve=1
						ORDER BY COALESCE( parent2_date, parent1_date, root_date ) DESC, 
						COALESCE( parent1_date, root_date ) DESC , root_date DESC 
						LIMIT 0, 6";
			$result = mysql_query($query) or die(mysql_error());
			$num_rows = mysql_num_rows($result);
			$pre_level = 0;
			if ($num_rows > 0){
				echo '<ul class="comment-lists">';
				while ($row = mysql_fetch_array($result)) {
					$imgUrl = $this->get_gravatar($row[email],36);
					$level = intval($row[level]);
					$said_on = date_create($row[created_on]);
					$reply = '';
					if ($level < 3) {
						$reply = '<a href="#reply" class="reply">Reply</a>';
					}
					if ($pre_level == 0) {
						$pre_level = $row[level];
						if ($pre_level > 1) {
							echo '<li class="list">';
							echo '<ul class="child">';
						}
						if ($pre_level > 2) {
							echo '<li class="list">';
							echo '<ul class="child">';
						}
					}
					else {
						if (($level - $pre_level) == 1) { //parent -> child
							echo '<ul class="child">';
						}
						//Child -> parent
						else {
							if (($pre_level - $level) == 1) {
								echo '</li>';
								echo '</ul>';
								echo '</li>';
							}
							else {
								if (($pre_level - $level) == 2) {
									echo '</li>';
									echo '</ul>';
									echo '</li>';
									echo '</ul>';
									echo '</li>';
								}
								else { //pre_level = level
									echo '</li>';
								}
							}
						}
						$pre_level = $level;
					}
					echo '<li class="list">';
?>
					<div class="comment-wrapper <?php echo 'level'.$level; ?>">
						<img src="<?php echo $imgUrl; ?>" alt="gravatar" class="author-email">
						<span class="comment-author"><?php echo $row[author] ?></span>
<?php
					if ($level > 1) {
?>
						<i aria-hidden="true" class="icon-forward" title="in reply to"></i>
<?php 
						echo $row[reply_to];
} 
?>
						<span class="bullet" aria-hidden="true">&#8226;</span>
						<span> <?php echo date_format($said_on,'d/m/Y h:m'); ?></span>
						<div class="comment-message-wrapper">
							<?php echo $row[message]; ?>
							<?php echo $reply; ?>
						</div><!-- End .comment-message-wrapper-->
						<div class="clear"></div>
					</div><!-- End .comment-wrapper-->
<?php
				}
				if ($pre_level == 1) {
					echo '</li>';
				}
				elseif ($pre_level == 2) {
					echo '</li>';
					echo '</ul>';
					echo '</li>';
				}
				else {
					echo '</li>';
					echo '</ul>';
					echo '</li>';
					echo '</ul>';
					echo '</li>';
				}

				echo '</ul>'; //End .comment-lists
			}
		}
		//Submit comment
		private function submit_comment()
		{

		}
		//Get gravatar
		private function get_gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
		{
		    $url = 'http://www.gravatar.com/avatar/';
		    $url .= md5( strtolower( trim( $email ) ) );
		    $url .= "?s=$s&d=$d&r=$r";
		    if ( $img ) {
		        $url = '<img src="' . $url . '"';
		        foreach ( $atts as $key => $val )
		            $url .= ' ' . $key . '="' . $val . '"';
		        $url .= ' />';
		    }
		    return $url;
		}
	}
?>